package com.fortune.controller;

import com.fortune.base.BaseAPIExcution;
import com.fortune.common.paging.PagingData;
import com.fortune.constants.Constants;
import com.fortune.constants.RoleConstants;
import com.fortune.dto.excution.COrderMgtExcution;
import com.fortune.dto.in.cOrderMgt.*;
import com.fortune.entity.DUserInfo;
import com.fortune.enums.COrderMgtEnum;
import com.fortune.service.*;
import com.fortune.vo.cOrderMgt.*;
import com.fortune.vo.cPriceMgt.CClientList;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Api(value = "cOrderMgt", description = "客户订单处理", tags = {"客户订单处理"})
@RestController
@RequestMapping("cOrderMgt")
@Validated
public class COrderMgtController {
	@Autowired
	private COrderBaseService cOrderBaseService;
	@Autowired
	private ClientService clientService;
	@Autowired
	private DstoreService dstoreService;
	@Autowired
	private PaymentMethodService paymentMethodService;
	@Autowired
	private MProductInfoService mProductInfoService;

	@ApiOperation(value = "客户订单列表", nickname = "cOrderList", notes = "", tags = {"@石匡"})
	@ApiImplicitParams({
		@ApiImplicitParam(name="page", value="第几页",paramType="query",dataType="int"),
		@ApiImplicitParam(name="length", value="每页容量",paramType="query",dataType="int"),
		@ApiImplicitParam(name="search", value="搜索关键词",paramType="query",dataType="string"),
		@ApiImplicitParam(name="approve", value="是否批准发货",paramType="query",dataType="string",allowableValues="YES,NO,ALL"),
		@ApiImplicitParam(name="delivery", value="是否已送达",paramType="query",dataType="string",allowableValues="YES,NO,ALL"),
		@ApiImplicitParam(name="account", value="是否已交账",paramType="query",dataType="string",allowableValues="YES,NO,ALL"),
		@ApiImplicitParam(name="driverId", value="司机id，为空代表查询所有司机",paramType="query",dataType="string"),
		@ApiImplicitParam(name="order", value="排序依据",paramType="query",dataType="string"),
		@ApiImplicitParam(name="type", value="排序类型(0升序,1降序,默认为1)",paramType="query",dataType="Integer",allowableValues = "0,1")
	})
    @GetMapping(value = "list",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<PagingData<COrder>> list(HttpServletRequest request,@ApiIgnore @Validated COrderListDto listDto) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String dealerId = dUserInfo.getdId();
		listDto.setPage((listDto.getPage()-1)*10);
		List<COrder> list = cOrderBaseService.queryOrderBaseList(dealerId,listDto);
		int count = cOrderBaseService.queryOrderBaseListCount(dealerId,listDto);
		PagingData<COrder> pagingData = new PagingData<>();
		pagingData.setData(list);
		pagingData.setRecordsTotal(count);
		return new COrderMgtExcution(pagingData, COrderMgtEnum.SUCCESS);
	}
	
	@ApiOperation(value = "添加客户订单", nickname = "cOrderAdd", notes = "", tags = {"@石匡"})
    @PostMapping(value = "add",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> add(HttpServletRequest request,@Validated @RequestBody COrderAddDto addDto) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String dealerId = dUserInfo.getdId();
		int num = cOrderBaseService.addOrderBase(dealerId,addDto);
		if (num < 1){
			return new COrderMgtExcution(null, COrderMgtEnum.FAILURE);
		}
		return new COrderMgtExcution(null, COrderMgtEnum.SUCCESS);
	}
	
	@ApiOperation(value = "编辑客户订单", nickname = "cOrderEdit", notes = "", tags = {"@石匡"})
    @PutMapping(value = "edit",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> edit(HttpServletRequest request,@Validated @RequestBody COrderEditDto editDto) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String dealerId = dUserInfo.getdId();
		int num = cOrderBaseService.editOrderBase(dealerId,editDto);
		if (num < 1){
			return new COrderMgtExcution(null, COrderMgtEnum.FAILURE);
		}
		return new COrderMgtExcution(null, COrderMgtEnum.SUCCESS);
	}
	
	@ApiOperation(value = "删除客户订单", nickname = "cOrderDelete", notes = "", tags = {"@石匡"})
	@ApiImplicitParams({
			@ApiImplicitParam(name="clientId", value="客户id",paramType="query",dataType="string"),
			@ApiImplicitParam(name="orderId", value="订单id",paramType="query",dataType="int")
	})
    @DeleteMapping(value = "delete",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> delete(HttpServletRequest request,@ApiIgnore @Validated @NotBlank(message="订单id不能为空") String orderId,
			@NotBlank(message="客户id不能为空") String clientId) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String dealerId = dUserInfo.getdId();
		int num = cOrderBaseService.deleteOrderBase(dealerId,orderId,clientId);
		if (num < 1){
			return new COrderMgtExcution(null, COrderMgtEnum.FAILURE);
		}
		return new COrderMgtExcution(null, COrderMgtEnum.SUCCESS);
	}
	
	@ApiOperation(value = "批准发货", nickname = "cOrderApprove", notes = "", tags = {"@石匡"})
    @PutMapping(value = "approve",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> approve(HttpServletRequest request, @Validated @RequestBody COrderApproveDto cOrderApproveDto) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String dealerId = dUserInfo.getdId();
		String userId = dUserInfo.getId();
		int num = cOrderBaseService.updateApproved(dealerId,userId,cOrderApproveDto);
		if (num < 1){
			return new COrderMgtExcution(null, COrderMgtEnum.FAILURE);
		}
		return new COrderMgtExcution(null, COrderMgtEnum.SUCCESS);
	}


	@ApiOperation(value = "客户订单详情", nickname = "cOrderDetails", notes = "", tags = {"@石匡"})
	@ApiImplicitParams({
			@ApiImplicitParam(name="orderId", value="订单id",paramType="query",dataType="string"),
			@ApiImplicitParam(name="clientId", value="客户id",paramType="query",dataType="string")
	})
	@GetMapping(value = "details",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<COrderDetails> details(HttpServletRequest request,@Validated @NotBlank(message="订单id不能为空") String orderId,
												  @NotBlank(message="客户id不能为空") String clientId) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String dealerId = dUserInfo.getdId();
		COrderDetails details = cOrderBaseService.queryDetails(dealerId,orderId,clientId);
		return new COrderMgtExcution<COrderDetails>(details, COrderMgtEnum.SUCCESS);
	}

	@ApiOperation(value = "司机", nickname = "driver", notes = "", tags = {"@石匡"})
	@GetMapping(value = "driver",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<List<COrderDriver>> driver(HttpServletRequest request) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String dealerId = dUserInfo.getdId();
		String driverId = RoleConstants.DRIVER_ID;
		List<COrderDriver> list = cOrderBaseService.queryDriverList(dealerId,driverId);
		return new COrderMgtExcution<List<COrderDriver>>(list, COrderMgtEnum.SUCCESS);
	}

	@ApiOperation(value = "客户信息", nickname = "clientUser", notes = "", tags = {"@石匡"})
	@GetMapping(value = "clientUser",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<List<CClientList>> clientUser(HttpServletRequest request) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String companyId = dUserInfo.getmId();
		String dealerId = dUserInfo.getdId();
		List<CClientList> list = clientService.queryClientList(dealerId);
		return new COrderMgtExcution<List<CClientList>>(list, COrderMgtEnum.SUCCESS);
	}

	@ApiOperation(value = "开单人信息", nickname = "biller", notes = "", tags = {"@石匡"})
	@GetMapping(value = "biller",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<COrderBiller> biller(HttpServletRequest request) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String dealerId = dUserInfo.getdId();
		String userId = dUserInfo.getId();
		COrderBiller biller = cOrderBaseService.queryBiller(dealerId,userId);
		return new COrderMgtExcution<COrderBiller>(biller, COrderMgtEnum.SUCCESS);
	}

	@ApiOperation(value = "经销商仓库", nickname = "dStore", notes = "", tags = {"@石匡"})
	@GetMapping(value = "dStore",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<List<COrderStore>> dStore(HttpServletRequest request) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String dealerId = dUserInfo.getdId();
		List<COrderStore> stores = dstoreService.queryStore(dealerId);
		return new COrderMgtExcution<List<COrderStore>>(stores, COrderMgtEnum.SUCCESS);
	}

	@ApiOperation(value = "付款方式列表", nickname = "getPaymentMethodList", notes = "" ,tags = {"@石匡"})
	@GetMapping(value = "getList",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<List<CPaymentMethod>> list() {
		List<CPaymentMethod> list = paymentMethodService.queryOrderPaymentMethod();
		return new COrderMgtExcution<List<CPaymentMethod>>(list, COrderMgtEnum.SUCCESS);
	}



	@ApiOperation(value = "获取经销商商品", nickname = "dealerProductList", notes = "", tags={ "@石匡" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="companyId", value="厂商id(自营时传入空字符串)",paramType="query",dataType="string"),
			@ApiImplicitParam(name="id", value="客户id",paramType="query",dataType="string")
	})
	@GetMapping(value = "dealerProductList",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<List<DealerProductList>> DealerproductList(HttpServletRequest request, @ApiIgnore @Validated COrderDealerProduceDto cOrderDealerProduceDto) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String dealerId = dUserInfo.getdId();
		List<DealerProductList> list = new ArrayList<>();
		if ("".equals(cOrderDealerProduceDto.getCompanyId())){
			list = mProductInfoService.queryDProduce(dealerId,cOrderDealerProduceDto);
		}else {
			list = mProductInfoService.queryMProduce(dealerId,cOrderDealerProduceDto);
		}
		return new COrderMgtExcution<List<DealerProductList>>(list, COrderMgtEnum.SUCCESS);
	}

}
